package com.rotten;

import imageLoader.DrawableManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Movieprofile extends Activity {
	public Movie movie = new Movie();
	private Client client = new Client();	
	private int idMovie = 0;
	DrawableManager draw = new DrawableManager();
	AlertDialog.Builder alert;
	EditText input;
	String tweet = "";
	Favorites favorites = new Favorites();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// full screen
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			idMovie = extras.getInt("movie");
		}

		setContentView(R.layout.movie);

		LinearLayout principal = (LinearLayout) findViewById(R.id.movie_info);
		principal.removeAllViews();

		String json = client.get_Movie(idMovie);
		movie = client.parse_movieResults(json);

		ImageView image = (ImageView) findViewById(R.id.profile);
		TextView title = (TextView) findViewById(R.id.titulo);
		TextView r1 = (TextView) findViewById(R.id.rating_critics);
		TextView r2 = (TextView) findViewById(R.id.rating_audience);
		image.setImageResource(R.drawable.icon);
		// image.setImageDrawable(Rotten.getImag(movie.getPoster_profile()));
		if(movie.getPoster_thumbnail() !=null) draw.fetchDrawableOnThread(movie.getPoster_profile(), image);

		image.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(Movieprofile.this, FullScreenIMG.class);
				i.putExtra("title", movie.getTitle());
				i.putExtra("audience", movie.getAudience_score());
				i.putExtra("critics", movie.getCritics_score());
				i.putExtra("link_img", movie.getPoster_detailed());
				startActivity(i);
			}
		});

		// se tive menos de 60 muda a imgagen
		ImageView c = (ImageView) findViewById(R.id.movie_img_critics);
		if (movie.getCritics_score() < 60)
			c.setImageResource(R.drawable.rotten);
		if (movie.getCritics_score() >= 95)
			c.setImageResource(R.drawable.superfresh);

		if (movie.getCritics_score() < 0)
			r1.setText("? %");
		else
			r1.setText(movie.getCritics_score() + "%");
		r2.setText(movie.getAudience_score() + "%");
		title.setText(movie.getTitle() + " (" + movie.getYear() + ")");

		// Log.v("MyActivity", movie.getSynopsis());
		// adiciona a sinopse

		TextView synompsis = new TextView(this);
		try {
			String s = movie.getSynopsis();
			if (!s.equals("")){
				synompsis.setText(movie.getSynopsis() + "\n");
				principal.addView(synompsis);
			}
			else
				synompsis.setText("Synopsis NOT FOUND\n");
		} catch (Exception E) {
			synompsis.setText("Synopsis NOT FOUND\n");
		}

		// adiciona o genero
		TextView genero = new TextView(this);
		String g = "Genre: ";
		for (int i = 0; i < movie.getGenres().size(); ++i)
			g += movie.getGenres().get(i) + ",";
		g += "\n";
		genero.setText(g);
		principal.addView(genero);

		// botao "tweet this"

		// cria alert dialog
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		input = new EditText(this);
		input.setMinEms(50);
		input.setHint("Insert your comment here");

		builder.setView(input);
		builder.setPositiveButton("Submit",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						tweet = input.getText().toString().trim();
						Toast.makeText(getApplicationContext(), tweet,
								Toast.LENGTH_SHORT).show();
						input.setText("");
					}
				});

		builder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						input.setText("");
						dialog.cancel();
					}
				});
		final AlertDialog alert = builder.create();

		Button tweet = (Button) findViewById(R.id.tweet_this);
		tweet.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				alert.show();
			}
		});
		
		// adiciona e remove dos favoritos
		final CheckBox fav = (CheckBox) findViewById(R.id.movie_favorites);
		
		if(favorites.existMovieFavorites(movie.getId())){
			fav.setChecked(true);
		}
		
		fav.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(fav.isChecked()){
					Main.favoritos.add(movie);
					favorites.saveFavorite(Main.favoritos);
					Toast.makeText(getApplicationContext(), "Ja tem " + Main.favoritos.size() + " favoritos", Toast.LENGTH_LONG).show();
				}
				else {
					if(favorites.existMovieFavorites(movie.getId())){
						favorites.removeFavorite(movie.getId());
					}
				}
			}
		});

	}

	// Menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.home: {
			Intent i = new Intent(Movieprofile.this, Main.class);
			startActivity(i);
			break;
		}
		case R.id.sair: {
			System.exit(0);
			break;
		}
		}
		return true;
	}
}
